package com.buba.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.buba.mapper.CalculationrulesMapper;
import com.buba.mapper.SalaryitemMapper;
import com.buba.mapper.SchemeMapper;
import com.buba.service.SchemeService;
import entity.salary.Calculationrules;
import entity.salary.Salaryitem;
import entity.salary.Scheme;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import entity.salary.vo.selAllCalculationrulesVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author beisheng
 * @since 2023-03-22
 */
@Service
public class SchemeServiceImpl extends ServiceImpl<SchemeMapper, Scheme> implements SchemeService {

    @Resource
    private SchemeMapper schemeMapper;

    @Resource
    private CalculationrulesMapper calculationrulesMapper;

    @Resource
    private SalaryitemMapper salaryitemMapper;
    /* *
     * @Author: beisheng
     * @Descriprion: 查询所有薪资方案
     * @Date: 2023/3/22 15:24
     * @Return: common.R
     * @Param: []
     */

    @Override
    public List<Scheme> selAllScheme() {
        List<Scheme> schemes = schemeMapper.selectList(new QueryWrapper<Scheme>().eq("delete_flag", 0));
        return schemes;
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 根据选择的薪资方案查询薪资项目
     * @Date: 2023/3/22 15:33
     * @Return: common.R
     * @Param: [salaryitemId]
     */

    @Override
    public List<selAllCalculationrulesVo> selAllCalculationrules(Integer salaryitemId) {
        List<Calculationrules> calculationrules = calculationrulesMapper.selectList(new QueryWrapper<Calculationrules>().eq("salaryitem_id", salaryitemId));
        List<selAllCalculationrulesVo> selAllCalculationrulesVos = new ArrayList<>();
        for (Calculationrules calculationrule : calculationrules) {
            selAllCalculationrulesVo selAllCalculationrulesVo = new selAllCalculationrulesVo();
            BeanUtils.copyProperties(calculationrule,selAllCalculationrulesVo);
            String groupId = calculationrule.getGroupId();
            List<String> list = Arrays.asList(groupId);
            List<Salaryitem> salaryitems = salaryitemMapper.selectList(new QueryWrapper<Salaryitem>().eq("delete_flag", 0).in("xz_id", list));
            List<String> a = new ArrayList<>();
            for (Salaryitem salaryitem : salaryitems) {
                a.add(salaryitem.getProjectName());
            }
            selAllCalculationrulesVo.setSalaryItem(a);
            selAllCalculationrulesVos.add(selAllCalculationrulesVo);
        }
        return selAllCalculationrulesVos;
    }
}
